package com.library.uiframework.fragment.ptr;

import android.view.View;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * 在此写用途
 *
 * @version V1.0 <描述当前版本功能>
 * @FileName: com.yxwframework.view.fragment.PtrViewHelper.java
 * @author: zhiwei
 * @date: 2016-04-24 23:42
 */
public class PtrViewHelper {
    private final PtrClassicFrameLayout mPtrFrameLayout;
    private final OnPtrHandlerListener mHandlerListener;

    public PtrViewHelper(PtrClassicFrameLayout ptrFrameLayout, OnPtrHandlerListener handlerListener) {
        mPtrFrameLayout = ptrFrameLayout;
        mHandlerListener = handlerListener;
        init();
    }

    private void  init(){
        mPtrFrameLayout.setLastUpdateTimeRelateObject(this);
        mPtrFrameLayout.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                mHandlerListener.onLoadMoreBegin(frame);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                mHandlerListener.onRefreshBegin(frame);
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                if(mHandlerListener.onInterceptRefresh()){
                    return false;
                }
                return super.checkCanDoRefresh(frame, content, header);
            }

            @Override
            public boolean checkCanDoLoadMore(PtrFrameLayout frame, View content, View footer) {
                if(mHandlerListener.onInterceptLoadMore()){
                    return false;
                }
                return super.checkCanDoLoadMore(frame, content, footer);
            }
        });
    }

    public static interface OnPtrHandlerListener{
        /**
         * 加载更多
         * @param frameLayout
         */
        void onLoadMoreBegin(PtrFrameLayout frameLayout);

        /**
         * 下拉刷新加载数据
         * @param frameLayout
         */
        void onRefreshBegin(PtrFrameLayout frameLayout);

        /**
         * 是否拦截加载更多功能，
         * @return true，屏蔽加载更多功能， false 由框架决定
         */
        boolean onInterceptLoadMore();

        /**
         * 是否拦截下拉刷新功能，
         * @return true，屏蔽加载更多功能， false 由框架决定
         */
        boolean onInterceptRefresh();
    }
}
